/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.andersen.web.utils;

import com.andersen.database.entity.Roles;
import com.andersen.database.entity.StateEntity;
import com.andersen.database.entity.UserEntity;
import java.text.SimpleDateFormat;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Component;

/**
 *
 * @author Corvus 11
 */
@Component
public class DataTableJsonUtils {
    
    public static final String RESULT_ATTRIBUTE_ROW_ID = "DT_RowId";
    
    protected static final SimpleDateFormat localDateFormat = new SimpleDateFormat("dd.MM.yyyy");
    
    public JSONObject UserToDataTableRow(UserEntity user) {
         JSONObject result = new JSONObject();
            result.accumulate(RESULT_ATTRIBUTE_ROW_ID, user.getId());
            result.accumulate("login", user.getLogin());
            result.accumulate("nameSurnamePatronymic", user.getEmployee().getSurnameNamePatronymic());
            result.accumulate("active", user.isActive());
            result.accumulate("skype", user.getEmployee().getSkype());
            result.accumulate("phone", user.getEmployee().getPhone());
            result.accumulate("hr", user.getRoles().contains(Roles.HR));
            result.accumulate("manager", user.getRoles().contains(Roles.MANAGER));
        return result;
    }
    
    public JSONObject StateToDataTableRow(StateEntity state) {
        JSONObject result = new JSONObject();
            result.accumulate(RESULT_ATTRIBUTE_ROW_ID, state.getComponent().getId());
            result.accumulate("model", state.getComponent().getDescription());
            if (state.getComponent().getWarrantyEndDate() != null)
                result.accumulate("warrantyEnd", localDateFormat.format(state.getComponent().getWarrantyEndDate()));
            else
                result.accumulate("warrantyEnd", "Без гарантии");
            result.accumulate("serial", state.getComponent().getSerial());
            result.accumulate("deleted", state.getComponent().getDeleted());
            String snp = state.getEmployee().getSurnameNamePatronymic();
            if ("".equals(snp)) snp = "Нет";
            result.accumulate("surnameNamePatronymic", snp);
            result.accumulate("officeName", state.getEmployee().getOffice().getName());
            result.accumulate("status", state.getStatusDescription());
       return result;
    }
}
